//
//  FzhNavigationController.swift
//  FirstSwiftProduct
//
//  Created by 范泽华 on 2017/10/10.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit

class FzhNavigationController: UINavigationController,UINavigationControllerDelegate,UIGestureRecognizerDelegate {

//    override class func initialize(){
//        
//        let bar = UINavigationBar.appearance()
//        bar.setBackgroundImage(UIImage.init(named: "navigation_bar_background"), for: UIBarMetrics.default)
//        bar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
//        let item = UIBarButtonItem.appearance()
//        item.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)
//        item.tintColor = UIColor.white
//        
//    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.white;
        self.navigationBar.isHidden = true
        
        
        if self.responds(to: #selector(getter: interactivePopGestureRecognizer)) {
            self.interactivePopGestureRecognizer?.delegate = (self as UIGestureRecognizerDelegate);
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /**
     *  重写这个方法目的：能够拦截所有push进来的控制器
     *
     *  @param viewController 即将push进来的控制器
     */

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0 {// 这时push进来的控制器，不是第一个子控制器（不是根控制器
            // 设置导航栏上面的内容
            // 设置左边返回按钮
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.item_fzh_Create(target: self, action: #selector(back), image: "arrow_left_w", highImage: "arrow_left_w")
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func back(){
        self.popViewController(animated: true)
    }
    
    
//代理
    // mark - navigationDelegate 实现此代理方法也是为防止滑动返回时界面卡死
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if self.responds(to: #selector(getter: interactivePopGestureRecognizer)) {
            self.interactivePopGestureRecognizer?.isEnabled = true;
        }
    }
    
   // mark - UIGestureRecognizerDelegate 在根视图时不响应interactivePopGestureRecognizer手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.viewControllers.count == 1 {
            return false
        }else{
            return true
        }
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
